package com.shoulder.boot.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shoulder.boot.system.dto.DeptAddDTO;
import com.shoulder.boot.system.dto.DeptModDTO;
import com.shoulder.boot.system.dto.DeptQueryDTO;
import com.shoulder.boot.system.entity.Dept;
import com.shoulder.boot.system.service.DeptService;
import com.shoulder.boot.system.vo.DeptVo;
import com.shoulder.core.api.ApiResult;
import com.shoulder.core.fetch.BaseIds;
import com.shoulder.core.page.PageSchema;
import com.shoulder.log.annotation.LogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/api/v1/system/dept")
public class DeptController {

    private final DeptService deptService;

    /**
     * 获取部门树
     *
     * @return ApiResult<List < Dept>>
     */
    @GetMapping("/tree")
    @LogAnnotation(type = "获取部门树", record = false)
    public ApiResult<List<Dept>> tree() {
        List<Dept> deptTree = this.deptService.tree();
        return ApiResult.ok(deptTree);
    }


    /**
     * 添加部门信息
     *
     * @param addDTO 部门添加实体
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @PostMapping
    @LogAnnotation(type = "添加部门信息")
    public ApiResult<Object> add(@RequestBody @Validated DeptAddDTO addDTO) {
        this.deptService.add(addDTO);
        return ApiResult.ok();
    }

    /**
     * 修改部门信息
     *
     * @param modDTO 部门编辑实体
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @PutMapping
    @LogAnnotation(type = "修改部门信息")
    public ApiResult<Object> mod(@RequestBody @Validated DeptModDTO modDTO) {
        this.deptService.mod(modDTO);
        return ApiResult.ok();
    }

    /**
     * 查询部门信息分页数据
     *
     * @param queryDTO 部门查询实体
     * @return ApiResult<PageSchema < DeptVo>>
     * @author 596767880@qq.com
     */
    @LogAnnotation(type = "部门信息分页查询", record = false)
    @PostMapping("/page")
    public ApiResult<PageSchema<DeptVo>> pageQuery(@RequestBody DeptQueryDTO queryDTO) {
        Page<DeptVo> page = this.deptService.pageQuery(queryDTO);
        return ApiResult.ok(new PageSchema<>(page.getTotal(), page.getRecords()));
    }

    /**
     * 查询部门信息列表数据
     *
     * @param queryDTO 查询实体
     * @return ApiResult<List < DeptVo>>
     * @author 596767880@qq.com
     */
    @LogAnnotation(type = "部门信息列表查询", record = false)
    @PostMapping("/list")
    public ApiResult<List<DeptVo>> list(@RequestBody DeptQueryDTO queryDTO) {
        return ApiResult.ok(this.deptService.listQuery(queryDTO));
    }

    /**
     * 批量删除部门信息
     *
     * @param baseIds id集合
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @PostMapping("/rm")
    @LogAnnotation(type = "批量删除部门信息")
    public ApiResult<Object> delete(@RequestBody @Validated BaseIds<String> baseIds) {
        this.deptService.batchDel(baseIds);
        return ApiResult.ok();
    }


    /**
     * 根据id删除部门信息
     *
     * @param id 部门信息id
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @DeleteMapping("/{id}")
    @LogAnnotation(type = "删除部门信息")
    public ApiResult<Object> del(@PathVariable String id) {
        this.deptService.del(id);
        return ApiResult.ok();
    }


    /**
     * 批量查询部门信息
     *
     * @param baseIds id集合
     * @return ApiResult<List < DeptVo>>
     * @author 596767880@qq.com
     */
    @PostMapping("/look")
    @LogAnnotation(type = "批量查询部门信息", record = false)
    public ApiResult<List<DeptVo>> looks(@RequestBody BaseIds<String> baseIds) {
        return ApiResult.ok(this.deptService.looks(baseIds));
    }

    /**
     * 根据id获取部门信息
     *
     * @param id 部门信息id
     * @return ApiResult<DeptVo>
     * @author 596767880@qq.com
     */
    @GetMapping("/{id}")
    @LogAnnotation(type = "根据id查询部门信息", record = false)
    public ApiResult<DeptVo> look(@PathVariable String id) {
        return ApiResult.ok(this.deptService.look(id));
    }
}